package com.pinecone.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;


import com.pinecone.common.Constant.RoleConstant;
import com.pinecone.common.annotation.CurrentUser;
import com.pinecone.common.exception.MinerException;
import com.pinecone.common.validator.group.UpdateGroup;
import com.pinecone.dto.LiveRoomTabDTO;
import com.pinecone.dto.UserDetailsDTO;
import com.pinecone.entity.LiveRoomEntity;
import com.pinecone.service.LiveRoomService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.pinecone.entity.LiveRoomTabEntity;
import com.pinecone.service.LiveRoomTabService;
import com.pinecone.common.utils.PageUtils;
import com.pinecone.common.utils.Query;
import com.pinecone.common.utils.R;


/**
 * 直播间菜单栏
 *
 * @author hushangjie
 * @email 979783618@qq.com
 * @date 2017-09-27 21:17:58
 */
@RestController
@RequestMapping("live_room_tab")
public class LiveRoomTabController {
    @Autowired
    private LiveRoomTabService liveRoomTabService;

    @Autowired
    private LiveRoomService liveRoomService;

    /**
     * 获取直播间菜单
     * @param liveRoomId
     * @return
     */
    @GetMapping("/{live_room_id}")
    public R getTabs(@PathVariable(value = "live_room_id",required = true) String liveRoomId){
        List<LiveRoomTabEntity> liveRoomTabEntities = liveRoomTabService.queryByLiveRoomId(liveRoomId);
        return R.ok().put("menus",liveRoomTabEntities);
    }

    /**
     * 增加菜单
     * @param currentUser
     * @param dto
     * @return
     */
    @PreAuthorize("hasAnyRole('ROLE_ORGHOST','ROLE_ORGMASTER')")
    @PostMapping("")
    public R save(@CurrentUser UserDetailsDTO currentUser, @RequestBody @Validated LiveRoomTabDTO dto) {
        //判断操作的直播间是否是自己的
        LiveRoomEntity liveRoomEntity = liveRoomService.queryObject(dto.getLiveRoomId());
        if (liveRoomEntity.getUserId() != currentUser.getId()){
            throw new MinerException("无权操作",403);
        }
        LiveRoomTabEntity liveRoomTabEntity = new LiveRoomTabEntity();
        BeanUtils.copyProperties(dto, liveRoomTabEntity);
        liveRoomTabService.save(liveRoomTabEntity);
        return R.ok().put("liveTab",liveRoomTabEntity);
    }

    /**
     * 修改
     */
    @PreAuthorize("hasAnyRole('ROLE_ORGHOST','ROLE_ORGMASTER')")
    @PutMapping("")
    public R update(@RequestBody @Validated({UpdateGroup.class}) LiveRoomTabEntity liveRoomTabEntity, @CurrentUser UserDetailsDTO currentUser) {
        LiveRoomEntity liveRoomEntity = liveRoomService.queryObject(liveRoomTabEntity.getLiveRoomId());
        if (liveRoomEntity == null) {
            throw new MinerException("直播间不存在");
        }
        if (currentUser.getId() != liveRoomEntity.getUserId()) {
            //只能删除自己创建的直播间菜单
            return R.error(403, "无权操作");
        }
        liveRoomTabService.update(liveRoomTabEntity);
        return R.ok();
    }

    /**
     * 删除
     */
    @PreAuthorize("hasAnyRole('ROLE_ORGHOST','ROLE_ORGMASTER')")
    @DeleteMapping("")
    public R delete(@RequestParam(value = "liveroom_tab_id", required = true) Long liveRoomTabId, @CurrentUser UserDetailsDTO currentUser) {
        LiveRoomTabEntity liveRoomTabEntity = liveRoomTabService.queryObject(liveRoomTabId);
        if (liveRoomTabEntity == null) {
            throw new MinerException("菜单不存在");
        }
        LiveRoomEntity liveRoomEntity = liveRoomService.queryObject(liveRoomTabEntity.getLiveRoomId());
        if (liveRoomEntity == null) {
            throw new MinerException("直播间不存在");
        }
        if (currentUser.getId() != liveRoomEntity.getUserId()) {
            return R.error(403, "无权操作");

        }
        liveRoomTabService.delete(liveRoomTabId);
        //有待商榷
        return R.ok();

    }

    /**
     * 列表
     */
    /*@GetMapping("")

    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        if (params.get("page") != null && params.get("limit") != null) {
            List<LiveRoomTabEntity> liveRoomTabList = liveRoomTabService.queryList(query);
            int total = liveRoomTabService.queryTotal(query);

            PageUtils pageUtil = new PageUtils(liveRoomTabList, total, query.getLimit(), query.getPage());

            return R.ok().put("page", pageUtil);
        } else {
            List<LiveRoomTabEntity> liveRoomTabList = liveRoomTabService.queryList(query);
            return R.ok().put("list", liveRoomTabList);
        }

    }*/


    /**
     * 信息
     */
    /*@GetMapping("/{liveRoomTabId}")
    public R info(@PathVariable("liveRoomTabId") Long liveRoomTabId) {
        LiveRoomTabEntity liveRoomTab = liveRoomTabService.queryObject(liveRoomTabId);

        return R.ok().put("liveRoomTab", liveRoomTab);
    }*/

}
